# 板块联动，用Python对所有的概念板块进行监控，龙一封板，马上下单抢龙二、龙三
import os
import time

import requests

base_path = os.path.join(os.getcwd().split('StockMonitor')[0] + 'StockMonitor')
from utils import doc_util, msg_util


def rt_dynamic(code):
    time_stamp = int(round(time.time() * 1000))
    url_i = 'http://push2.eastmoney.com/api/qt/clist/get?ut=bd1d9ddb04089700cf9c27f6f7426281&' \
            'pi=0&pz=8&po=1&fid=f62&fs=b:' + code + \
            '&=&fields=f1,f2,f12,f13,f14,f62,f152,f128,f3,f62,f184,f66,f69,f72,f75,f78,f81,f84,f87' \
            '&cb=jQuery1124013032222692515671_' + str(time_stamp) + '&_=' + str(time_stamp + 127)
    resp = requests.get(url_i)
    data = eval(str(resp.content.decode('utf-8')).split('(')[1].split(')')[0].replace('null', '""'))['data']
    total = int(data['total'])
    if total == 0:
        return
    stock_list = data['diff']
    return stock_list
    # for rank in stock_dict:
    #     # 只有8枚
    #     print(stock_dict[rank])


'''
f2:最新价格(精确到分)
f3:涨跌幅(*100)
f12:代码
f14:名称
f62:主力净流入金额
f66:超大单净流入金额
f69:超大订单流入占比
f72:大单净流入金额
f75:大订单流入占比
f78:中单净流入金额
f81:中订单流入占比
f84:小单净流入金额
f87:小订单流入占比
'''


def catch_the_dragon():
    monitor_path = os.path.join(base_path)  # todo
    doc_file = open(monitor_path, mode='r', encoding='utf-8')
    doc_list = doc_util.get_doc_info(doc_file)
    for plate_info in doc_list:
        plate = eval(plate_info)
        stock_dict = rt_dynamic(plate['code'])
        count = 0
        main_inflows_amount = 0
        sb_inflows_amount = 0
        b_inflows_amount = 0
        sb_inflows_prop = 0
        b_inflows_prop = 0
        for key in stock_dict:
            stock = stock_dict[key]
            price_limit = float(int(stock['f3']))
            if price_limit >= 998:
                count += 1
            main_inflows_amount += float(stock['f62'])
            sb_inflows_amount += float(stock['f66'])
            b_inflows_amount += float(stock['f72'])
            sb_inflows_prop += float(stock['f69'])
            b_inflows_prop += float(stock['f75'])
        sb_inflows_prop = sb_inflows_prop / 8
        b_inflows_prop = b_inflows_prop / 8
        if count >= 1:
            for key in stock_dict:
                info = stock_dict[key]['f12'] + '\t' + stock_dict[key]['f14']
                info += "\t上升幅度" + str(stock_dict[key]['f3'] / 100) + '%'
                info += "\t主力资金流入" + str(stock_dict[key]['f62'])
                print(info)
            msg = plate['name'] + '板块异动拉升\n'
            msg += '主力流入' + str(main_inflows_amount) + '\n'
            msg += '超大单流入' + str(sb_inflows_amount) + '\n'
            msg += '大单流入' + str(b_inflows_amount) + '\n'
            msg += '超大单流入占比' + str(sb_inflows_prop / 100) + '\n'
            msg += '大单流入占比' + str(b_inflows_prop / 100) + '\n'
            print(msg)
            # msg_util.win_msg('紧急提示', msg, color='g')


if __name__ == '__main__':
    print('start')
    catch_the_dragon()
    print('end')
